/*
 * Digitized Emergency Plan Management System
 * Copyright(c) 2007, Kehao Safety, Inc.
 * @author nixin@kehaoinfo.com
 * 
 * http://www.kehaosafe.com
 */
 
 	/**
	 * 功能:检查并实时显示textarea的输入字数
	 * 参数:
	 *   textarea: 要显示字数的文本域
	 *   maxLength: 文本域最大长度
	 *   input:文本域字符显示区
	 * 示例:
	 *     <textarea name="txt" rows="5" clos="70" onPropertyChange="checkLength(this,1024,'input')"></textarea>
	 *	   最大长度为<b>1024</b>个字符。已输入<span id="input"><b>0</b></span>个字符。
	 */
	function checkLength(textarea, maxLength, input)
	{
		var show = document.getElementById(input) || document.getElementsByName(input)[0];
		var value = trim(textarea.value);
		if( value.length > maxLength)
		{
			show.innerHTML = "<font color=red><b>"+ value.length + "</b></font>";
		}
		else
		{
			show.innerHTML = "<b>"+ value.length + "</b>";
		}
	}

/*
*检查是否输入整数
*/
function isInteger(value){
	return /^(-|\+)?\d+$/.test(value);
}

/*
*检查是否输入数字
*/
function isNumber(value){
        return !isNaN(value);
}

/*
 *中文转unicode编码
 */
function chr2Unicode(str)
{
	if ('' != str) 
	{
		var st, t, i;
		st = '';
		for (i = 1; i <= str.length; i++)
		{
			t = str.charCodeAt(i - 1).toString(16);
			if (t.length < 4)
			while(t.length <4)
				t = '0'.concat(t);
			t = t.slice(2, 4).concat(t.slice(0, 2))
			st = st.concat(t);
		}
		return(st.toUpperCase());
	}
	else
	{
		return('');
	}
}

/**
*unicode编码转中文
*/
function unicode2Chr(str) {
	if ('' != str) {
		var st, t, i
		st = '';
		for (i = 1; i <= str.length/4; i ++){
			t = str.slice(4*i-4, 4*i-2);
			t = str.slice(4*i-2, 4*i).concat(t);
			st = st.concat('%u').concat(t);
		}
		st = unescape(st);
		return(st);
	}
	else
		return('');
}

/**
 * 取字符串长度,一个中文两个字节长
*/
String.prototype.len=function()
{
	return this.replace(/[^\x00-\xff]/g,"**").length;
}

/**
 *日期格式验证
*/
function isValidDate(oDateTime)
{
    //对日期格式进行验证 要求为1900-2099年  格式为 yyyy-mm-dd[ hh:mi:ss]  并且可以正常转换成正确的日期
    var pat_hd=/^((19|20)\d{2}-((0[1-9]{1})|(1[0-2]{1}))-((0[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))){1}(\s\d{2}:\d{2}:\d{2})?$/;
	try
	{
		if(!pat_hd.test(oDateTime))
		{
			throw "日期非法！";
		}

		var arr_dt=oDateTime.split(" ");
		if(arr_dt[0]=='')
		{
			throw "日期非法！";
		}

		var oDate=arr_dt[0];
		var arr_hd=oDate.split("-");
		var dateTmp;
		dateTmp= new Date(arr_hd[0],parseFloat(arr_hd[1])-1,parseFloat(arr_hd[2]));
		
		if(dateTmp.getFullYear()!=parseFloat(arr_hd[0]) || dateTmp.getMonth()!=parseFloat(arr_hd[1]) -1 || dateTmp.getDate()!=parseFloat(arr_hd[2])) 
		{
			throw "日期非法！";
		}

		if(arr_dt[1] && arr_dt[1]!='')
        {
			var oTime=arr_dt[1];
            var arr_ht=oTime.split(":");
            dateTmp.setHours(arr_ht[0],arr_ht[1],arr_ht[2]);
			if(dateTmp.getHours()!=parseFloat(arr_ht[0]) || dateTmp.getMinutes()!=parseFloat(arr_ht[1]) || dateTmp.getSeconds()!=parseFloat(arr_ht[2]))
			{
				throw "日期非法！";
			}
		}
	}
	catch(ex)
	{
		if(ex.description)
		{
			return false;
		}
		else
		{
			return false;
		}
	}
	return true;
}

/**
* 设置Html标签Select的选项
* @param selectName
* @param selectValue
*/
function setSelectValue(selectName,selectValue)
{
	var objSelect = document.getElementsByName(selectName)[0];
	objSelect.value = selectValue;
}

/**
 * 去除字符串的首尾空白
 */
 function trim(value)
 {
 	var trimRe = /^\s+|\s+$/g;
 	return String(value).replace(trimRe, "");
 }
 
 /**
 * 用于Submit中等待响应的全局变量
 */
BakupBodyFocus=null;
BackupWaitBodyContext=null;
CanCancelWait=true;

/**
 * Submit中等待响应显示信息
 */
function jscomLockScreenToWait(msg)
{
	if (BakupBodyFocus==null && BackupWaitBodyContext==null)
	{
		BakupBodyFocus = document.body.onfocus+'';
		BackupWaitBodyContext = document.body.oncontextmenu+'';
		document.body.onfocus = jscomLockScreenToWait;
		document.body.oncontextmenu = jscomCancelClick;
	}
	var div = document.all["divLockWaiting"];
	if (div+''=="undefined")
	{
		div = document.createElement("DIV");
		div.setAttribute("id","divLockWaiting");
		div.className = "WaitBox";
		//div.title="点击可以取消";
		div.style.padding = 10;
		div.style.paddingLeft = 30;
		div.style.paddingRight = 30;
		div.innerHTML = msg;
		document.body.appendChild(div);
	}
	var x =(document.body.clientWidth-div.offsetWidth)/2;
	var y = (document.body.clientHeight-div.offsetHeight)/2;
	div.style.pixelLeft = x;
	div.style.pixelTop = y;
	div.style.visibility = "visible";
	//div.onclick = jscomCancelClick; // 暂时屏蔽用户可取消屏幕锁定
	div.setCapture();
}

/**
 * 取消Submit中等待响应的显示信息
 */
function jscomUnlockScreenWait()
{
	var div = document.all["divLockWaiting"];
	if (div+''=="undefined")
		return;
	div.style.visibility = "hidden";
	document.body.onfocus = BakupBodyFocus;
	document.body.oncontextmenu = BackupWaitBodyContext;
	BakupBodyFocus = null;
	BackupWaitBodyContext = null;
	document.releaseCapture();
}

/**
 * 取消Submit中等待响应的显示信息
 */
function jscomCancelClick()
{
	var elem = event.srcElement;
	if (CanCancelWait==true && elem.className=='WaitBox'){
		jscomUnlockScreenWait();
		event.cancelBubble = false;
		return false;
	}
	return false;
}

function isFaxNum(value)
{
   //var objRe = /^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
    var objRe = /^(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/;
	if(value != "" && objRe.test(value))
	{		
		return true;
	}else
	{
		return false;
	}
}

function isMobileNum(value)
{
  // var objRe = /^((\(\d{3}\))|(\d{3}\-))?0?13\d{9}$/;
   var objRe = /^\d{1,14}$/;
	if(value != "" && objRe.test(value))
	{		
		return true;
	}else
	{
	    return false;
	}
}

/*
function isOtherNum(value)
{
   var objRe = /^\d{3,4}$/;
	if(value != "" && objRe.test(value))
	{		
		return true;
	}
	else
	{
		return false;
	}
}
*/

function isPhoneNum(value)
{
	if(isMobileNum(value) || isFaxNum(value))
	{		
		return true;
	}
	else
	{
	    return false;
	}
}

//过滤隐藏
function SearchTableControl(tableId,blankId,lableId)
{
	var tableId = document.getElementById(tableId);
	var blankId = document.getElementById(blankId);
	var lableId = document.getElementById(lableId);

	if(tableId.style.display == "")
	{
		tableId.style.display = "none";
		blankId.style.display = "none";
		lableId.innerHTML="显示查询";
	}
	else if(tableId.style.display == "none")
	{
		tableId.style.display = "";
		blankId.style.display = "";
		lableId.innerHTML="隐藏查询";
	}
	else
	{
		alert("请初始化页面！");
	}
}

/*
说明：切换隐藏/显示“查询条件表单”
*/
function switchQuery()
{
    var normalQuery = document.getElementById('normalQuery');
    var advQuery = document.getElementById('advQuery');
    var queryBtn = document.getElementById('queryBtn');

    if (advQuery.style.display == '')
    {
        normalQuery.style.display = '';
        advQuery.style.display = 'none';
        queryBtn.innerText = '高级查询';
    }
    else
    {
        normalQuery.style.display = 'none';
        advQuery.style.display = '';
        queryBtn.innerText = '常用查询';
    }
}

/*
	验证实数，整数部分不超过m位，小数部分不超过n位
*/
function isReal(value,m,n)
{
	//var regex = /^\d{1,9}([\.]\d{1,4})?$/
	var regex =new RegExp("^\\d{1,"+m+"}([\\.]\\d{1,"+n+"})?$");
	if(value != "" && regex.test(value))
	{		
		return true;
	}
	else
	{
	    return false;
	}
}
//验证value在min和max之间
function between(min,max,value)
{
		if (value>=min && value<=max)
		{
			return true;
		}
		else
		{
			return false
		}
}

//验证是否正整数
function isPlusInteger(value)
{
	if (isInteger(value))
	{
		if (value>=0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function isPlusIntegerBetween(min,max,value)
{
	if (isPlusInteger(value))
	{
		if (between(min,max,value))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function isPostCode(value)
{
	if ((isPlusInteger(value)) && (value.length=6))
	{
		return true;
	}
	else
	{
		return false;
	}
}
